package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import obj.Utilisateur;

public class UtilisateurDAO extends DAO3<Utilisateur> {

		public boolean create(Utilisateur obj) {
			try {
				PreparedStatement ps = this.connect
						.prepareStatement("INSERT INTO user_video (login,mdp,config) values ('"+obj.getId()+"','"+obj.getMdp()+"','"+obj.getConf()+"');");
				ps.executeUpdate();
				ps.close();
				
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}
			return true;
		}

		public Utilisateur find(String id, String mdp) {
			Utilisateur u = new Utilisateur();
			try {
				ResultSet rs = this.connect.createStatement(
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_UPDATABLE).executeQuery(
						"SELECT * FROM user_video WHERE login='" + id + "' AND mdp='"+ mdp +"'");
				if (rs.first()) {
					u = new Utilisateur(id, rs.getString("mdp"), rs.getString("config"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return u;
		}

		public boolean update(Utilisateur obj) {
			try {
				PreparedStatement ps = this.connect
						.prepareStatement("UPDATE user_video SET mdp='"+obj.getMdp()+"' AND config='"+obj.getConf()+"' WHERE login ='"+obj.getId()+"'");
				ps.executeUpdate();
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}
			return true;
		}

		public void delete(Utilisateur obj) {
			try {
				PreparedStatement ps = this.connect
						.prepareStatement("DELETE FROM user_video WHERE login ='"+obj.getId()+"'");
				ps.executeUpdate();
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
}
